import 'KeySettings.dart';
import 'LedSetting.dart';

class ADVInfoResponse {
  int pid;
  int vid;
  int uid;
  int leftDeviceQuantity;
  bool isLeftCharging;
  int rightDeviceQuantity;
  bool isRightCharging;
  int chargingBinQuantity;
  bool isDeviceCharging;
  String deviceName;
  int micChannel;
  int workModel;
  List<KeySetting> keySettingsList;
  List<LedSetting> ledSettingsList;
  int inEarSettings;
  String language;
  List<int> modes;
  int xmOpCode; // ✅ 新增属性

  ADVInfoResponse({
    required this.pid,
    required this.vid,
    required this.uid,
    required this.leftDeviceQuantity,
    required this.isLeftCharging,
    required this.rightDeviceQuantity,
    required this.isRightCharging,
    required this.chargingBinQuantity,
    required this.isDeviceCharging,
    required this.deviceName,
    required this.micChannel,
    required this.workModel,
    required this.keySettingsList,
    required this.ledSettingsList,
    required this.inEarSettings,
    required this.language,
    required this.modes,
    this.xmOpCode = -1, // ✅ 默认值为 -1
  });

  factory ADVInfoResponse.fromJson(Map<String, dynamic> json) {
    return ADVInfoResponse(
      pid: json['pid'] ?? 0,
      vid: json['vid'] ?? 0,
      uid: json['uid'] ?? 0,
      leftDeviceQuantity: json['leftDeviceQuantity'] ?? 0,
      isLeftCharging: json['isLeftCharging'] ?? false,
      rightDeviceQuantity: json['rightDeviceQuantity'] ?? 0,
      isRightCharging: json['isRightCharging'] ?? false,
      chargingBinQuantity: json['chargingBinQuantity'] ?? 0,
      isDeviceCharging: json['isDeviceCharging'] ?? false,
      deviceName: json['deviceName'] ?? '',
      micChannel: json['micChannel'] ?? 0,
      workModel: json['workModel'] ?? 0,
      keySettingsList: (json['mKeySettingsList'] as List<dynamic>?)
          ?.map((e) => KeySetting.fromJson(Map<String, dynamic>.from(e)))
          .toList() ??
          [],
      ledSettingsList: (json['mLedSettingsList'] as List<dynamic>?)
          ?.map((e) => LedSetting.fromJson(Map<String, dynamic>.from(e)))
          .toList() ??
          [],
      inEarSettings: json['inEarSettings'] ?? 0,
      language: json['language'] ?? '',
      modes: (json['modes'] as List<dynamic>?)?.map((e) => e as int).toList() ?? [],
      xmOpCode: json['xmOpCode'] ?? -1, // ✅ 解析 JSON 中的 xmOpCode
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'pid': pid,
      'vid': vid,
      'uid': uid,
      'leftDeviceQuantity': leftDeviceQuantity,
      'isLeftCharging': isLeftCharging,
      'rightDeviceQuantity': rightDeviceQuantity,
      'isRightCharging': isRightCharging,
      'chargingBinQuantity': chargingBinQuantity,
      'isDeviceCharging': isDeviceCharging,
      'deviceName': deviceName,
      'micChannel': micChannel,
      'workModel': workModel,
      'mKeySettingsList': keySettingsList.map((e) => e.toJson()).toList(),
      'mLedSettingsList': ledSettingsList.map((e) => e.toJson()).toList(),
      'inEarSettings': inEarSettings,
      'language': language,
      'modes': modes,
      'xmOpCode': xmOpCode, // ✅ 添加到 JSON 中
    };
  }
}
